package com.zbkj.front.controller;

import com.alipay.api.AlipayApiException;
import com.zbkj.common.request.order.ClientOrderSaveRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.response.order.OrderSucceResponse;
import com.zbkj.front.service.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin("*")
@RestController
@RequestMapping("api/front/pay")
@Api(tags = "客户端订单(充值)服务")
public class PayController {

    @Resource
    private PaymentService alipayPaymentService;
    @Resource
    private PaymentService wechatPaymentService;

    @GetMapping("/a")
    public String a() throws AlipayApiException {
        return wechatPaymentService.pay(null);
    }


    @ApiOperation(value = "微信支付")
    @PostMapping("/wechat/payment")
    public CommonResult<OrderSucceResponse> wechatPayment(@RequestBody @Validated ClientOrderSaveRequest saveRequest) throws AlipayApiException {
        Map<String, Object> params = new HashMap<>();
        params.put("saveRequest", saveRequest);
        return CommonResult.success(wechatPaymentService.wxpay(params));
    }

    @ApiOperation(value = "支付宝支付")
    @PostMapping("/ali/payment")
    public CommonResult<OrderSucceResponse> aliPayment(@RequestBody @Validated ClientOrderSaveRequest saveRequest) throws AlipayApiException {
        Map<String, Object> params = new HashMap<>();
        params.put("saveRequest", saveRequest);
        return CommonResult.success(alipayPaymentService.pay(params));
    }


    @PostMapping("/wechat/notify")
    public String wechatNotify(HttpServletRequest request, HttpServletResponse response) {
        return wechatPaymentService.callback(request, response);
    }

    @PostMapping("/ali/notify")
    public String aliNotify(HttpServletRequest request, HttpServletResponse response) {
        return alipayPaymentService.callback(request, response);
    }
}
